@echo off
chcp 65001 >nul
echo ==========================================
echo 负载均衡功能专项测试
echo ==========================================
echo.

echo [测试1] 基础连通性测试
echo -----------------------------------------
echo 🔍 检查Provider服务：
echo Provider 11000:
curl -s "http://localhost:11000/user/getUserById/1" 2>nul | findstr "userName"
echo Provider 11001:
curl -s "http://localhost:11001/user/getUserById/1" 2>nul | findstr "userName"
echo.

echo [测试2] Gateway认证测试
echo -----------------------------------------
echo 🔐 测试认证（应该返回401）：
curl -s -w "状态码: %%{http_code}" "http://localhost:9000/provider/user/getUserById/1" 2>nul
echo.
echo.

echo [测试3] 负载均衡测试 - Provider路由
echo -----------------------------------------
echo 🔀 多次请求观察负载均衡效果（注意端口变化）：
echo.

for /l %%i in (1,1,8) do (
    echo 请求 %%i:
    curl -s -H "Authorization: Bearer user123token" "http://localhost:9000/provider/user/getUserById/1" 2>nul | findstr "userName" || echo "  ❌ 请求失败"
    timeout /t 1 /nobreak >nul 2>nul
)
echo.

echo [测试4] 负载均衡测试 - 实例信息接口
echo -----------------------------------------
echo 🎯 测试专门的负载均衡演示接口：
echo.

for /l %%i in (1,1,6) do (
    echo 实例请求 %%i:
    curl -s -H "Authorization: Bearer user123token" "http://localhost:9000/provider/user/instance-info" 2>nul | findstr "port\|message" || echo "  ❌ 请求失败"
    timeout /t 1 /nobreak >nul 2>nul
)
echo.

echo [测试5] 负载均衡测试 - 专用演示路由
echo -----------------------------------------
echo 🚀 测试lb-demo路由（无熔断器干扰）：
echo.

for /l %%i in (1,1,4) do (
    echo lb-demo请求 %%i:
    curl -s -H "Authorization: Bearer user123token" "http://localhost:9000/lb-demo/user/instance-info" 2>nul | findstr "port\|instance_id" || echo "  ❌ 请求失败"
    timeout /t 1 /nobreak >nul 2>nul
)
echo.

echo ==========================================
echo 负载均衡测试完成！
echo ==========================================
echo.
echo 📊 负载均衡验证标准：
echo 1. ✅ 如果看到端口在11000和11001之间变化，说明负载均衡正常
echo 2. ✅ 如果用户名显示不同端口号，说明请求分发到不同实例
echo 3. ✅ 如果instance_id或port字段值不同，说明负载均衡生效
echo 4. ❌ 如果总是同一个端口，可能是：
echo    - 只有一个Provider实例在运行
echo    - 负载均衡策略配置问题
echo    - 服务注册有问题
echo.
echo 🔍 故障排查：
echo - 检查Eureka: http://localhost:18000
echo - 查看服务状态: http://localhost:9000/gateway/services
echo - 检查Gateway日志中的负载均衡日志
echo.
pause 